cmake_minimum_required (VERSION 3.26)
project(GuoEngine VERSION 0.1.0)
set(CMAKE_CXX_STANDARD 20)

set(Application EngineEditor)


set(EngineDirectory ${CMAKE_CURRENT_LIST_DIR}/../../Engine)





file(GLOB_RECURSE AllCppFiles ./Source/*.cpp )

#TODO use ${CMAKE_CXX_COMPILER_ID} to optional add Natvis file

if(WIN32)
  add_executable(${Application} ${AllCppFiles}  "${EngineDirectory}/Modules/EngineCore/WuhuaEngine.natvis" "${CMAKE_CURRENT_LIST_DIR}/Icon.rc"  )
else()
  add_executable(${Application} ${AllCppFiles})
endif()

target_link_libraries(${Application} PUBLIC 
  EngineCore 
  EngineEditorLib
)

target_include_directories(${Application} PUBLIC 
  ${CMAKE_CURRENT_LIST_DIR}/Source/
)

#set DEBUG Macro
target_compile_definitions(${Application} PUBLIC "$<$<CONFIG:DEBUG>:DEBUG>")


add_custom_target(Reflected_${Application}
    USES_TERMINAL 
    COMMAND  $<TARGET_FILE:EngineReflectionTool>  
    ${EngineDirectory}/Modules/EngineCore/Source "EngineCore"
    ${EngineDirectory}/Modules/EngineEditorLib/Source ${Application}
    ${CMAKE_CURRENT_LIST_DIR}/Source ${Application}
)

add_dependencies(${Application} Reflected_${Application})


add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/ScriptProjectTemplate)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/ProjectPackager)

#add dependency
add_dependencies(${Application} ScriptProjectTemplate ProjectPackager)

# copy build result of project template to output directory
if(WIN32)
  set(TemplateTargetDir $<TARGET_FILE_DIR:${Application}>/ProjectTemplate/Win64/)

elseif (LINUX)
  set(TemplateTargetDir $<TARGET_FILE_DIR:${Application}>/ProjectTemplate/Linux/)
endif()

if(WIN32)
    set(PlatformType Win64)
elseif(LINUX)
    set(PlatformType Linux)
endif()
#get dlls of target EnginePrecompiledLibs 

#获取引擎依赖的dll 和 pdb 文件
get_target_property(PrecompiledLinkLibs EnginePrecompiledLibs DLLS)

set(TemplateDlls
  $<TARGET_FILE:EngineCore>
  $<TARGET_FILE:ScriptProjectTemplate>
  $<TARGET_FILE:ProjectTemplateMainModule>

  #引擎依赖的dll
  $<TARGET_FILE:rtaudio>
  ${PrecompiledLinkLibs}


)

if(WIN32)
  #pdb 文件
  list(APPEND TemplateDlls
    
    $<$<CONFIG:Debug>:$<TARGET_PDB_FILE:EngineCore>>
    $<$<CONFIG:Debug>:$<TARGET_PDB_FILE:ScriptProjectTemplate>>
    $<$<CONFIG:Debug>:$<TARGET_PDB_FILE:ProjectTemplateMainModule>>
  )
endif()

add_custom_command(TARGET ${Application} POST_BUILD

    COMMAND ${CMAKE_COMMAND} -E make_directory ${TemplateTargetDir}
   
    #copy exe dlls
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
    ${TemplateDlls}
    ${TemplateTargetDir}
)

